use bitflags::bitflags;
use std::fmt;

bitflags! {
    #[repr(transparent)]
    // #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    #[derive(Debug, Clone, Copy, PartialEq)]
    struct MyFlags: u32 {
        const FLAG_A       = 0b00000001;
        const FLAG_B       = 0b00000010;
        const FLAG_C       = 0b00000100;
        const FLAG_ABC     = Self::FLAG_A.bits()
                           | Self::FLAG_B.bits()
                           | Self::FLAG_C.bits();
    }
}

impl MyFlags {
    pub fn clear(&mut self) -> &MyFlags {
        // 使用静态方法 MyFlags::all() 来获取所有标志的组合
        self.remove(MyFlags::all());
        self
    }
}

impl fmt::Display for MyFlags {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:032b}", self.bits())
    }
}

fn main() {
    println!("all = {}", MyFlags::all());
    let e1 = MyFlags::FLAG_A | MyFlags::FLAG_C;
    let e2 = MyFlags::FLAG_B | MyFlags::FLAG_C;
    assert_eq!((e1 | e2), MyFlags::FLAG_ABC);   
    assert_eq!((e1 & e2), MyFlags::FLAG_C);    
    assert_eq!((e1 - e2), MyFlags::FLAG_A);    
    assert_eq!(!e2, MyFlags::FLAG_A);           

    let mut flags = MyFlags::FLAG_ABC;
    assert_eq!(format!("{}", flags), "00000000000000000000000000000111");
    assert_eq!(format!("{}", flags.clear()), "00000000000000000000000000000000");
    assert_eq!(format!("{:?}", MyFlags::FLAG_B), "MyFlags(FLAG_B)");
    assert_eq!(format!("{:?}", MyFlags::FLAG_A | MyFlags::FLAG_B), "MyFlags(FLAG_A | FLAG_B)");

    println!("{}", e1);
    println!("{}", e2);
    println!("{:?}", MyFlags::FLAG_ABC);
    println!("{}", MyFlags::FLAG_ABC);

    /*
        打印输出如下
        00000000000000000000000000000101
        00000000000000000000000000000110
        MyFlags(FLAG_A | FLAG_B | FLAG_C)
        00000000000000000000000000000111
     */


}
